package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicProGetApplyDAO;
import com.cssy.yd.logic.dao.LogicProGetApplyItemDAO;
import com.cssy.yd.logic.entity.LogicProGetApplyDO;
import com.cssy.yd.logic.entity.LogicProGetApplyItemDO;
import com.cssy.yd.logic.service.ILogicProGetApplyService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicProGetApplyService")
public class LogicProGetApplyServiceImpl implements ILogicProGetApplyService {

	private static final Logger logger = Logger.getLogger(LogicProGetApplyServiceImpl.class);

	@Resource(name = "logicProGetApplyDAO")
	private LogicProGetApplyDAO logicProGetApplyDAO;

	@Resource(name = "logicProGetApplyItemDAO")
	private LogicProGetApplyItemDAO logicProGetApplyItemDAO;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;

	@Override
	public SearchResult findListByExample(SearchConditions scs)throws SyBaseException {
		SearchResult rs = new SearchResult(scs);
		List<LogicProGetApplyDO> list = logicProGetApplyDAO.find(scs);
		rs.setResults(list);
		rs.setRecordCount(logicProGetApplyDAO.count(scs));// 添加分页计算 add by huang.xuming[2013-11-09]
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return logicProGetApplyDAO.deleteLogicProGetApplyDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicProGetApplyDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicProGetApplyDAO.updateLogicProGetApplyDO(t);
	}

	@Override
	public LogicProGetApplyDO save(LogicProGetApplyDO t, User user)
			throws SyBaseException {
//		t.setDcruser(user.getName());
//		t.setDctime(new Date());
		logicProGetApplyDAO.insertLogicProGetApplyDO(t);
		return null;
	}

	@Override
	public LogicProGetApplyDO findListById(String id) throws SyBaseException {
		return logicProGetApplyDAO.findLogicProGetApplyDOByPrimaryKey(id);
	}

	@Override
	public void proGetSave(HttpServletRequest request, LogicProGetApplyDO curAP)
			throws SyBaseException {
		try {
			String itemdata = request.getParameter("addressInfos");
			List<LogicProGetApplyItemDO> swItemList = JSONArray.parseArray(itemdata,LogicProGetApplyItemDO.class);
			/*
			 * 获取操作 0为保存(草稿状态) 
			 * 1为提交(待审核状态) 
			 * 2为审核通过(订单变为完成状态) 
			 * 3为完结订单（强制将订单处理为全部领料）
			 */
			String paramflag = request.getParameter("billStatus");
			
			if (StringUtils.isBlank(curAP.getId())) {
				String id = IDCodeTypeUtil.getId();
				curAP.setId(id);
				curAP.setBillStatus(Short.parseShort(paramflag));
				curAP.setGetProCode(syCodeService.getCode("WS","WS",SyCode.DATE_YYMM,5));
				curAP.setDcruser(SessionUtil.getUser(request).getName());
				curAP.setDctime(new Date());
				curAP.setState(0);
				curAP.setIsnotck(0);
				logicProGetApplyDAO.insertLogicProGetApplyDO(curAP);
				 for(int i=0;i<swItemList.size();i++){
					 swItemList.get(i).setGetProId(id);
					 swItemList.get(i).setId(IDCodeTypeUtil.getId());
					 logicProGetApplyItemDAO.insertLogicProGetApplyItemDO(swItemList.get(i));
				 }
			}else{
				// 修改
				int result = 0;
				if ("1".equals(paramflag)) {// 提交二级审核
					curAP.setBillStatus(Short.parseShort(paramflag));
					curAP.setDuuser(SessionUtil.getUser(request).getName());
					curAP.setDutime(new Date());
					result=logicProGetApplyDAO.updateLogicProGetApplyDO(curAP);
					if(1==result){
						logicProGetApplyItemDAO.deleteLogicProGetApplyPrimaryKey(curAP.getId());
						for(int i=0;i<swItemList.size();i++){
							swItemList.get(i).setGetProId(curAP.getId());
							swItemList.get(i).setId(IDCodeTypeUtil.getId());
							logicProGetApplyItemDAO.insertLogicProGetApplyItemDO(swItemList.get(i));
						}
					}else{
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
				}
				if("2".equals(paramflag)){
					curAP.setBillStatus(Short.parseShort(paramflag));
					curAP.setDuuser(SessionUtil.getUser(request).getName());
					curAP.setDutime(new Date());
					result=logicProGetApplyDAO.updateLogicProGetApplyDO(curAP);
					if(0==result){
						throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
					}
				}
				if("3".equals(paramflag)){
					LogicProGetApplyDO proget = logicProGetApplyDAO.findLogicProGetApplyDOByPrimaryKey(curAP.getId());
					if(proget.getIsnotck()==2){
						throw new SyBaseException("该领料申请单已经完结!");
					}else{
						curAP.setBillStatus(proget.getBillStatus());
						curAP.setIsnotck(2);
						curAP.setDuuser(SessionUtil.getUser(request).getName());
						curAP.setDutime(new Date());
						result=logicProGetApplyDAO.updateLogicProGetApplyDO(curAP);
						if(0==result){
							throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
						}
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}
}
